<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Org\Image;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class CompanyindexController extends Controller
{
    //加载视图
    public function index()
        {
        $list = \DB::table("companyindex")->get(); //获取所有收货地址信息
       
        return view("admin.companyindex.index",["list"=>$list]); //加载视图
       
        }
        
        
    //加载修改信息
    public function edit($id)
    {
        $mypic = \DB::table("companyindex")->where("id",$id)->first(); //获取要修改的公司信息
        return view("admin.companyindex.edit",["ci"=>$mypic]);
    }
    //执行修改
    public function update($id,Request $request)
    {
         if($request->hasFile("pic")){
            //获取上传信息
            $file = $request->file("pic");
            //确认上传的文件是否成功
            if($file->isValid()){
                $ext = $file->getClientOriginalExtension(); //获取上传文件名的后缀名
                //执行移动上传文件
                $time = time().rand(1000,9999).".".$ext;  //添加上传文件名
                //dd($time);
                $file->move("./uploads/companyindex/",$time);
                //进行图片缩放
                $img = new Image();
                $img->open("./uploads/companyindex/".$time)->thumb(60,60)->save("./uploads/companyindex/s_".$time);
            }
        }
        
        //判断是否修改图片
        if(!empty($time)){
            $pic = $request->only("content");
            $pic['picname'] = $time;
            //dd($pic);
            $name = \DB::table("companyindex")->where("id",$id)->pluck("picname");
            $id = \DB::table("companyindex")->where("id",$id)->update($pic);

            if($id){
                @unlink("./uploads/companyindex/".$name[0]);
                @unlink("./uploads/companyindex/s_".$name[0]);
                return "修改成功";
            }else{
                @unlink("./uploads/companyindex/".$time);
                @unlink("./uploads/companyindex/s_".$time);
                return "修改失败";
            }
        }else{
            $pic = $request->only("content");
            $id = \DB::table("companyindex")->where("id",$id)->update($pic);
            if($id){
                return '修改成功';
            }else{
                return "修改失败";
            }
        }
    }
    
    
    //添加
    public function create()
    {
        return view("admin.companyindex.add");
    }
    //执行要添加的信息
    public function store(Request $request)
    {
        if($request->hasFile("pic")){
            //获取上传信息
            $file = $request->file("pic");
            //确认上传的文件是否成功
            if($file->isValid()){
                $ext = $file->getClientOriginalExtension(); //获取上传文件名的后缀名
                //执行移动上传文件
                $time = time().rand(1000,9999).".".$ext;  //添加上传文件名\
                $file->move("./uploads/companyindex/",$time);
                $img = new Image();
                $img->open("./uploads/companyindex/".$time)->thumb(60,60)->save("./uploads/companyindex/s_".$time);
            }
        }
             //获取上传信息    
            $list = $request->only("content");
            $list['addtime']= time();
            $list['picname'] = $time;
            //dd($list);
            $id = \DB::table("companyindex")->insertGetId($list);
            if($id>0){
                echo "上传成功";
                
            }else{
                echo  "上传失败";
                
            }   
    }
    
    
    
    //删除
    public function destroy($id)
    {   
        $name = \DB::table("companyindex")->where("id",$id)->pluck("picname");
        \DB::table("companyindex")->where("id",$id)->delete();
        @unlink("./uploads/companyindex/".$name[0]);
        @unlink("./uploads/companyindex/s_".$name[0]);
        //重定向跳转
        return redirect()->back();
    }
    
}
